package com.gome.ocean.controller.yugong;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.Page;
import com.gome.ocean.common.exception.EntityCreateException;
import com.gome.ocean.common.utils.ResponsePage;
import com.gome.ocean.dao.model.yugong.YgManagerBO;
import com.gome.ocean.service.base.AuthorityBaseController;
import com.gome.ocean.service.yugong.YgManagerService;

/**
 * Created by liuqingxia on 2016/6/28.
 */
@Controller
@RequestMapping("/yugong/machine")
public class MachineController extends AuthorityBaseController {

    private static final Logger LOG = LoggerFactory.getLogger(MachineController.class);

    @Autowired
    private YgManagerService ygManagerService;

    @RequestMapping(value = "/toInsertPage", method = RequestMethod.GET)
    public ModelAndView toInsertPage(ModelAndView mav) {
        List<String> projectLists = this.getCurrentUserProjects();
        mav.addObject("projectLists", projectLists);
        mav.setViewName("yugong/machine/insert");
        return mav;
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public ResponsePage insert() {
        ResponsePage response = getResponsePage();
        YgManagerBO ygManagerBO;
        try {
            ygManagerBO = extractManagerFromReq();
        } catch (EntityCreateException e) {
            return response.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            return response.setErrorMsg(EXCEPTION_MSG);
        }
        if (null == ygManagerBO) {
            return response.setErrorMsg("愚公机器创建失败，请检查参数后重新提交");
        }
        try {
            if (ygManagerService.insertYgManagerBO(ygManagerBO)) {
                return response.setSuccessMsg(INSERT_SUCCESS);
            }
            return response.setErrorMsg(INSERT_FAILURE);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            return response.setErrorMsg(EXCEPTION_MSG);
        }
    }

    /**
     * 获取http参数并组装YgManagerBO
     * 
     * @return
     * @throws EntityCreateException
     */
    private YgManagerBO extractManagerFromReq() throws EntityCreateException {
        String ygIp = getParamValue("ygIp");
        if (StringUtils.isBlank(ygIp)) {
            throw new EntityCreateException("机器iP不能为空");
        }
        String ygLocation = getParamValue("ygLocation");
        if (StringUtils.isBlank(ygLocation)) {
            throw new EntityCreateException("机器目录不能为空");
        }
        Integer isOccupy = getIntParamValue("isOccupy");
        Integer isDisabled = getIntParamValue("isDisabled");
        String projectName = getParamValue("projectName");
        YgManagerBO ygManagerBO = new YgManagerBO();
        ygManagerBO.setYgIp(ygIp);
        ygManagerBO.setYgLocation(ygLocation);
        ygManagerBO.setIsDisabled(isDisabled);
        ygManagerBO.setIsOccupy(isOccupy);
        ygManagerBO.setProjectName(projectName);
        return ygManagerBO;
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ResponseBody
    public ResponsePage remove(Long id) {
        ResponsePage response = getResponsePage();
        try {
            if (ygManagerService.removeYgManagerBOById(id)) {
                return response.setSuccessMsg(REMOVE_SUCCESS);
            }
            return response.setErrorMsg(REMOVE_FAILURE);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            return response.setErrorMsg(EXCEPTION_MSG);
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResponsePage update() {
        ResponsePage response = getResponsePage();
        YgManagerBO ygManagerBO;
        try {
            ygManagerBO = extractManagerFromReq();
        } catch (EntityCreateException e) {
            return response.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            return response.setErrorMsg(EXCEPTION_MSG);
        }
        if (null == ygManagerBO) {
            return response.setErrorMsg("更新机器记录失败，请检查参数设置");
        }
        ygManagerBO.setId(getLongParamValue("id"));
        try {
            if (ygManagerService.updateYgManagerBO(ygManagerBO)) {
                return response.setSuccessMsg(UPDATE_SUCCESS);
            } else {
                return response.setErrorMsg(UPDATE_FAILURE);
            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            return response.setErrorMsg(EXCEPTION_MSG);
        }
    }

    @RequestMapping(value = "/findById", method = RequestMethod.POST)
    @ResponseBody
    public ResponsePage findById(Long id) {
        ResponsePage response = getResponsePage();
        try {
            YgManagerBO ygManagerBO = ygManagerService.findYgManagerBOById(id);
            if (null == ygManagerBO) {
                return response.setErrorMsg(FIND_NO_RESULTS);
            }
            return response.setSingleObject(ygManagerBO, FIND_SUCCESS);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            return response.setErrorMsg(EXCEPTION_MSG);
        }
    }

    @RequestMapping(value = "/toFindPage", method = RequestMethod.GET)
    public ModelAndView toFindPage(ModelAndView mav) {
        mav.addObject("projectLists", this.getCurrentUserProjects());
        mav.setViewName("yugong/machine/find");
        return mav;
    }

    @RequestMapping(value = "/find", method = RequestMethod.POST)
    @ResponseBody
    public ResponsePage find(HttpServletRequest request) {
        ResponsePage response = getResponsePage();
        Integer pageNum = this.getPageNum();
        Integer pageSize = this.getPageSize();
        Page<YgManagerBO> page = null;
        try {
            if (isAdmin()) {
                page = ygManagerService.findAllYgManager(pageNum, pageSize);
            } else {
                List<String> projects = this.getCurrentUserProjects();
                page = ygManagerService.findAllYgManagerBOsByProjectNames(projects, pageNum, pageSize);
            }
            return response.setPage(page, "查询成功");
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            return getResponsePage().setErrorMsg(EXCEPTION_MSG);
        }
    }
}
